package com.jiantong.web.open.app.lawEnforcing;

import com.jiantong.lawEnforcing.model.AskAnswer;
import com.jiantong.lawEnforcing.service.AskAnswerService;
import com.jiantong.web.core.enums.ExceptionCode;
import io.terminus.boot.rpc.common.annotation.RpcConsumer;
import io.terminus.common.model.Response;
import io.terminus.pampas.openplatform.annotations.OpenBean;
import io.terminus.pampas.openplatform.annotations.OpenMethod;
import io.terminus.pampas.openplatform.exceptions.OPClientException;
import io.terminus.pampas.openplatform.exceptions.OPServerException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestMethod;

import java.util.List;

/**
 * 问答记录
 * Created by mz03 on 2017/8/15.
 */
@Slf4j
@OpenBean
public class AskAnswerApis {

    @RpcConsumer
    private AskAnswerService askAnswerService;


    /**
     * 根据id查询一条信息
     * @param id
     * @return
     */
    @OpenMethod(
            key = "find.askAnswer.by.id",
            paramNames = {"id"},
            httpMethods = RequestMethod.GET
    )
    public AskAnswer findById(Long id){
        Response<AskAnswer> response = askAnswerService.finById(id);
        if (!response.isSuccess()){
            log.error("failed to find news read by id = {}, cause : {}", id, response.getError());
            throw new OPServerException(response.getError());
        }
        return response.getResult();
    }


    /**
     * 根据ids查询一组
     * @param ids
     * @return
     */
    @OpenMethod(
            key = "find.askAnswer.by.id.list",
            paramNames = {"ids"},
            httpMethods = RequestMethod.GET
    )
    public List<AskAnswer> findById(List<Long> ids){
        Response<List<AskAnswer>> response = askAnswerService.finByIds(ids);
        if (!response.isSuccess()){
            log.error("failed to find AskAnswer list by ids = {}, cause : {}", ids, response.getError());
            throw new OPServerException(response.getError());
        }
        return response.getResult();
    }


    /**
     * 创建一条信息
     * @param askAnswer
     * @return
     */
    @OpenMethod(
            key = "create.askAnswer",
            paramNames = {"askAnswer"},
            httpMethods = RequestMethod.POST
    )
    public Long create(AskAnswer askAnswer){
        Response<Long> response = askAnswerService.create(askAnswer);
        if(!response.isSuccess()){
            log.error("failed to create askAnswer read by askAnswer = {}, cause : {}", askAnswer, response.getError());
            throw new OPServerException(response.getError());
        }
        return response.getResult();
    }


    /**
     * 创建一条问答记录,并更新到datas中对应的记录问答记录的ids中
     * @param askAnswer
     * @param lawCase
     * @param writType
     * @param idList
     * @return
     */
    @OpenMethod(
            key = "create.askAnswer.update.data",
            paramNames = {"askAnswer","lawCase","writType","idList"},
            httpMethods = RequestMethod.POST
    )
    public Long createAndList(AskAnswer askAnswer,Long lawCase,Integer writType,String idList){
        Response<Long> response = askAnswerService.createAndList(askAnswer,lawCase,writType,idList);
        if(!response.isSuccess()){
            log.error("failed to create askAnswer read by askAnswer = {}, cause : {}", askAnswer, response.getError());
            throw new OPServerException(response.getError());
        }
        return response.getResult();
    }

    /**
     * 更新单据
     */
    @OpenMethod(
            key = "update.ask.answer",
            paramNames = {"askAnswer"},
            httpMethods = RequestMethod.PUT
    )
    public Boolean update(AskAnswer askAnswer){
        if(askAnswer.getId() == null){
            log.error("id should be not null");
            throw new OPClientException(ExceptionCode.ID_NOT_EXIST.value(),
                    "id should be not null");
        }
        Response<Boolean> response = askAnswerService.update(askAnswer);
        if(!response.isSuccess()){
            log.error("failed to update askAnswer");
            throw new OPServerException(ExceptionCode.DATA_UPDATE_FAIL.value(),
                    "failed to update askAnswer");
        }
        return response.getResult();
    }


}
